<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<%@ page import="java.util.List" %>
<%@ page import="biggestloser.util.DatastoreUtil" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>

<%
  long currentWeek = DatastoreUtil.getCurrentWeek(); 
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <title>Biggest Loser Admin</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
  <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
  <script>
	$(function() {
	    $( "#tabs" ).tabs();
	});
	$(function() {
	    $( "#dialog" ).dialog();
	});
  </script>
</head>
<body>

  <%
    // See if we have a message to display, any message will be in the query
    // parameter "msg". If we find one create a <div> element named "dialog"
    // for jQuery to pickup
    if(request.getParameter("msg") != null && !request.getParameter("msg").equals(""))
    {
      String msg = request.getParameter("msg");
      out.println("<div id=\"dialog\" title=\"Message\">");
      out.println("  <p>" + msg + "</p>");
      out.println("</div>");
    }
  %>

  <h2>Biggest Loser Admin</h2>
  <div id="tabs">
  <ul>
    <li><a href="#tabs-1">All Users</a></li>
    <li><a href="#tabs-2">Add User</a></li>
    <li><a href="#tabs-3">User Status</a></li>
    <li><a href="#tabs-4">Send Message</a></li>
    <li><a href="#tabs-5">Update Results</a></li>
  </ul>
  <div id="tabs-1">
	<p>List of current users</p>
	<ul>
	  <%
	    List<Entity> users = DatastoreUtil.getUsers();
	    
	    for(Entity user : users)
	    {
	      String name   = (String) user.getProperty("name");
		  String email  = (String) user.getProperty("email");
		  String status = (String) user.getProperty("status");
		  
		  if(status == null)
		  {
		  	status = "enabled";
		  }
		  
		  out.println("<li>" + name + " [" + email + "] Status = " + status + "</li>");
	    }
	  %>
	</ul>
  </div>
  <div id="tabs-2">
    <p>Add a new user to the competition</p>
    <form name="newuser" action="/adminpost" method="POST">
      <p><label for="name">Name</label><input id="name" name="name" width="10"></input></p>
      <p><label for="email">Email</label><input id="email" name="email" width="10"></input></p>
      <p><button type="submit">Add</button></p>
    </form>
  </div>
  <div id="tabs-3">
    <p>Enable or Disable a User</p>
    <p>Note: Disabled users will still get weekly e-mails but will not show up on the main page until they are enabled.</p>
    <form name="disable" action="/adminpost" method="POST">
      <p><label for="email">Email</label><input id="email" name="email" width="10"></input></p>
      <p><label for="status">Status</label><input type="radio" name="status" value="enabled">Enabled</input><input type="radio" name="status" value="disabled">Disabled</input></p>
      <p><button type="submit">Update</button></p>
    </form>
  </div>
  <div id="tabs-4">
    <p>Send a message to all users</p>
    <form name="emailusers" action="/adminpost" method="POST">
      <p><label for="subject">Subject</label><input id="subject" name="subject" width="10"></input></p>
      <p><textarea id="message" name="message" rows="10" cols="50">Type message here</textarea></p>
      <p><button type="submit">Send</button></p>
    </form>
  </div>
  <div id="tabs-5">
    <p>Manually adjust a user's weight for a specific week</p>
    <form name="adjustweight" action="/adminpost" method="POST">
      <p><label for="email">Email</label><input id="email"    name="email"  width="10"></input></p>
      <p><label for="week">Week</label><input   id="week"     name="week"   width="2"></input></p>
      <p><label for="weight">Weight</label><input id="weight" name="weight" width="2"></input></p>
      <p><button type="submit">Update</button></p>
    </form>
  </div>
</div>
</body>
</html>
